<template>
  <div class="subject-contain">
    <el-form ref="formData" :model="formData" label-width="80px" :inline="true">
      <el-form-item prop="tag_name">
        <el-input
          v-model="formData.tag_name"
          placeholder="标签名称"
          style="width: 240px"
        ></el-input>
      </el-form-item>
      <el-form-item prop="subject_name">
        <el-select v-model="subjectValue" placeholder="归属学科">
          <el-option
            v-for="item in sub"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="directory_name">
        <el-select v-model="dictValue" placeholder="归属目录">
          <el-option
            v-for="item in directoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="is_show">
        <el-select
          v-model="formData.is_show"
          style="width: 240px; margin: 0 10px"
          placeholder="显示状态"
        >
          <el-option label="显示" value="1"></el-option>
          <el-option label="隐藏" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" plain @click="searchNews">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          style="margin-left: 70px"
          plain
          @click="$refs['add-tag'].dialogVisible = true"
          >创建标签</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table border :data="tableData" style="width: 100%; margin-top: 20px">
      <el-table-column label="序号" type="index" width="160"> </el-table-column>
      <el-table-column prop="tag_name" label="标签名称" width="180">
      </el-table-column>
      <el-table-column prop="directory_name" label="归属目录" width="180">
      </el-table-column>
      <el-table-column prop="subject_name" label="归属学科" width="180">
      </el-table-column>
      <el-table-column prop="username" label="创建人" width="180">
      </el-table-column>
      <el-table-column prop="is_show" label="是否显示" width="180">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.is_show"
            :value="row.is_show === 1 ? true : false"
            :active-value="1"
            :inactive-value="2"
            active-text="是"
            inactive-text="否"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column prop="address" label="操作">
        <template slot-scope="{ row }">
          <el-button
            @click="$refs['add-tag'].huixian(row)"
            type="primary"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            type="danger"
            @click="del(row.id)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="subject-pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <add-tag ref="add-tag" @update="initData"></add-tag>
  </div>
</template>

<script>
import {
  getTagAllNews,
  guishuSub,
  allDirector,
  allDirectory,
  delTagdirectory,
} from "@/api/subject";
import addTag from "./components/add-tag.vue";

export default {
  components: { addTag },
  name: "subject",
  data() {
    return {
      formData: {},
      value1: true,
      tableData: [],
      limit: 10,
      page: 1,
      total: 0,
      sub: [],
      mulu: [],
      subjectValue: "",
      dictValue: "",
    };
  },
  created() {
    this.initData();
    this.huoqu();
  },
  computed: {
    directoryList() {
      if (!this.subjectValue) {
        return [];
      } else {
        const res = this.sub.find((val) => val.id === this.subjectValue);
        console.log(res);
        return res.children;
      }
    },
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.limit = val;
      this.initData();
    },

    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.initData();
    },
    async initData() {
      let { data } = await getTagAllNews({
        limit: this.limit,
        page: this.page,
      });
      console.log(data.list[0].directory_name);
      this.tableData = data.list;
      this.total = data.total;
    },
    async huoqu() {
      let { data } = await allDirectory();

      console.log(data);
      this.sub = data;
    },
    async searchNews() {
      console.log(this.formData);
      console.log(this.subjectValue);
      console.log(this.dictValue);

      let { data } = await getTagAllNews({
        limit: this.limit,
        page: this.page,
        tag_name: this.formData.tag_name,
        is_show: this.formData.is_show,
        subject_name: this.subjectValue,
        directory_name: this.dictValue,
      });
      console.log(data);
    },
    async reset() {
      this.formData = {};
      this.subject_id = "";
      this.initData();
    },
    async del(id) {
      await this.$confirm("确定删除吗");
      console.log(id);
      await delTagdirectory(id);
      this.$message.success("删除成功");
      if (this.page !== 1 && this.tableData.length === 1) {
        this.page--;
      }
      this.initData();
    },
  },
};
</script>

<style>
.subject-contain {
  margin: 10px 10px;
}
.subject-pagination {
  display: flex;
  justify-content: end;
  margin: 20px 10px;
}
</style>
